package com.company.modeldesign;

public class MyHashSet {

    private static class Entity {
        int k;

        public Entity(int k) {
            this.k = k;
        }
    }

    Entity[] DEFAULT_ENTITY = new Entity[16];
    int size = 0;

    Entity[] entity = DEFAULT_ENTITY;

    float loadFactory = 0.75f;

    /**
     * Initialize your data structure here.
     */
    public MyHashSet() {

    }

    private int indexOf(int key) {
        return key%this.entity.length;
    }

    public void add(int key) {
        int i = indexOf(key);
        Entity target = this.entity[i];
        if (target == null) {
            entity[i] = new Entity(key);
            size++;
            resize();
        } else {
            if (target.k!=key) {
                while (target!=null&&i< entity.length-1){
                    target=entity[++i% entity.length];
                }
            }
        }
    }

    private void resize() {
        if (size == loadFactory * entity.length) {
            Entity[] old = entity;
            Entity[] newArray = new Entity[size << 1];
            for (Entity e : old) {
                add(e.k);
            }
        }
    }

    public void remove(int key) {
        int i = indexOf(key);
        Entity target = this.entity[i];
        if (target.k == key) {
            entity[i]=null;
            size--;
        }else {
            while (target!=null){
                target=entity[++i];
                if (target.k == key) {
                    entity[i]=null;
                    size--;
                    break;
                }
            }
        }
    }

}
